package edu.ccut.saturn.component.loader.node;

/**
 * 用于解析logic组件配置文件中“循环结束”结点的加载类。
 * <p>继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。<p>
 * 
 * @author 温建
 * 
 * @version 1.0 2008.8.18
 * 
 * @see <code>EndIteratorNodeInfo</code>
 */
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.node.EndIteratorNodeInfo;

public class EndIterationNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODETYPE_ITERATION_END = "end-iteration";

	public static final String KEY_ITERATION_START_ID = "start-id";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> endIterationNodeElements = root
				.elements(KEY_NODETYPE_ITERATION_END);

		List<INodeInfo> endIterationNodeInfos = new ArrayList<INodeInfo>(
				endIterationNodeElements.size());

		for (Element endIterationNodeElement : endIterationNodeElements) {
			String id = endIterationNodeElement.attributeValue(KEY_NODE_ID)
					.trim();

			String x = endIterationNodeElement.attributeValue(KEY_NODE_X)
					.trim();

			String y = endIterationNodeElement.attributeValue(KEY_NODE_Y)
					.trim();

			String nextId = endIterationNodeElement.attributeValue(
					KEY_NODE_NEXT).trim();

			String name = endIterationNodeElement
					.elementTextTrim(KEY_NODE_NAME);

			String description = endIterationNodeElement
					.elementTextTrim(KEY_NODE_DESCRIPTION);

			String startId = endIterationNodeElement
					.elementTextTrim(KEY_ITERATION_START_ID);

			logger.debug("  load iteration node id = \"" + id + "\" name = \"" 
					+ name + "\" description = \"" + description
					+ "\" nextId = \"" + nextId + "\" startId = \"" + startId + "\"");

			EndIteratorNodeInfo endIteratorNodeInfo = new EndIteratorNodeInfo(
					id, name, description, nextId, startId, x, y);

			endIterationNodeInfos.add(endIteratorNodeInfo);
		}
		return endIterationNodeInfos;
	}
}
